package cn.lyw.usermessage.service.service.impl;

import cn.common.beancopy.util.BeanCopierUtil;
import cn.common.restresult.util.RestResult;
import cn.common.restresult.util.ResultEnum;
import cn.hutool.core.lang.generator.ObjectIdGenerator;
import cn.lyw.usermessage.mapper.mapper.UserEncryptedMapper;
import cn.lyw.usermessage.mapper.pojo.SmallMiUserEncrypted;
import cn.lyw.usermessage.service.bo.AddUserEncryptedBO;
import cn.lyw.usermessage.service.bo.EditUserEncryptedBO;
import cn.lyw.usermessage.service.bo.GetUserEncryptedBO;
import cn.lyw.usermessage.service.domain.GetUserEncryptedDO;
import cn.lyw.usermessage.service.service.UserEncryptedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author lyw
 * @Description 用户密保逻辑层
 * @Date 9:22 2021/7/17
 * @Param
 * @return
 */
@Service
public class UserEncryptedServiceImpl implements UserEncryptedService {


    @Autowired
    private UserEncryptedMapper userEncryptedMapper;


    /**
     * @return
     * @Author lyw
     * @Description 根据用户id查询密保
     * @Date 9:21 2021/7/17
     * @Param
     */
    @Override
    public List<GetUserEncryptedDO> getUserEncrypted(GetUserEncryptedBO bo) {
        //转换类型
        SmallMiUserEncrypted smallMiUserEncrypted = BeanCopierUtil.copyObj(bo, SmallMiUserEncrypted.class);
        //根据用户id查询密保
        List<SmallMiUserEncrypted> smallMiUserEncrypteds = userEncryptedMapper.select(smallMiUserEncrypted);
        //将返回值转为do
        List<GetUserEncryptedDO> getUserEncryptedDOS = BeanCopierUtil.copyList(smallMiUserEncrypteds, GetUserEncryptedDO.class);
        return getUserEncryptedDOS;
    }


    /**
     * @return
     * @Author lyw
     * @Description 添加用户密保
     * @Date 9:21 2021/7/17
     * @Param
     */
    @Override
    public RestResult addUserEncrypted(AddUserEncryptedBO bo) {
        //先将参数转换
        SmallMiUserEncrypted smallMiUserEncrypted = BeanCopierUtil.copyObj(bo, SmallMiUserEncrypted.class);

        //自动生成密保id
        ObjectIdGenerator objectIdGenerator = new ObjectIdGenerator();
        String next = objectIdGenerator.next();
        smallMiUserEncrypted.setEncryptedId(next);

        //创建时间，创建者等
        smallMiUserEncrypted.setCreationDate(new Date());
        smallMiUserEncrypted.setCreatedBy(smallMiUserEncrypted.getMiId());
        smallMiUserEncrypted.setModifyDate(new Date());
        smallMiUserEncrypted.setModifieBy(smallMiUserEncrypted.getMiId());
        smallMiUserEncrypted.setVersion("1");

        //添加
        int i = userEncryptedMapper.insertSelective(smallMiUserEncrypted);
        //判断返回值
        if (i == 0) {
            return new RestResult(ResultEnum.ADD_ERROR);
        }
        return new RestResult(ResultEnum.ADD_SUCCESS);
    }


    /**
     * @return
     * @Author lyw
     * @Description 修改用户密保
     * @Date 9:21 2021/7/17
     * @Param
     */
    @Override
    public RestResult editUserEncrypted(EditUserEncryptedBO bo) {
        //转换参数
        SmallMiUserEncrypted smallMiUserEncrypted = BeanCopierUtil.copyObj(bo, SmallMiUserEncrypted.class);

        //修改更新者，更新时间
        smallMiUserEncrypted.setModifyDate(new Date());
        smallMiUserEncrypted.setModifieBy(smallMiUserEncrypted.getMiId());

        //版本
        SmallMiUserEncrypted smallMiUserEncrypted1 = userEncryptedMapper.selectByPrimaryKey(smallMiUserEncrypted.getEncryptedId());
        String version = smallMiUserEncrypted1.getVersion();
        int oldVersion = Integer.parseInt(version);
        Integer newVersion = oldVersion + 1;
        smallMiUserEncrypted.setVersion(newVersion.toString());

        //修改
        int i = userEncryptedMapper.updateByPrimaryKeySelective(smallMiUserEncrypted);
        //判断返回值
        if (i == 0) {
            return new RestResult(ResultEnum.EDIT_ERROR);
        }
        return new RestResult(ResultEnum.EDIT_SUCCESS);
    }


    /**
     * @return
     * @Author lyw
     * @Description 根据密保id删除用户密保
     * @Date 9:22 2021/7/17
     * @Param
     */
    @Override
    public RestResult delUserEncrypted(String encryptedId) {
        //删除
        int i = userEncryptedMapper.deleteByPrimaryKey(encryptedId);
        //判断返回值
        if (i == 0) {
            return new RestResult(ResultEnum.DEL_ERROR);
        }
        return new RestResult(ResultEnum.DEL_SUCCESS);
    }

}
